<?php
class ModelUser_management extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
   /**
    * Function authenticateUser($data)
    * used For fetching data
    * @author Biplab Biswas
    * @param $data array passing post value
    * perpose to check authenticate user data
    */
    
    function AddAdminUser($data)
    {
	
	echo $sql = "INSERT INTO ".ADMIN."  SET
				`username`				= '".$data['username']."',
				`password`				= '".$data['password']."',
				`email_address` 		= '".$data['email_address']."',
				`full_name`				= '".$data['full_name']."',
				`status`				= '".$data['status']."',
				`date_added`			= NOW() ";
		
		$result = $this->db->query($sql);
	 // $this->db->insert(ADMIN,$data);
             //echo $this->db->insert_id(); die;
        if($this->db->insert_id()){
            return true;
        }
        else{
            return false;
	}
    }
    
    function getList(&$config,&$start,&$param)
	{
	    
		// GET DATA FROM GET/POST  OR   SESSION ====================
		$Count = 0;
		$page = $this->uri->segment(3,0); // page
		$isSession = $this->uri->segment(4,0); // read data from SESSION or POST     (1 == POST , 0 = SESSION )

		$start = 0;

		$sortType 		= $param['sortType'];
		$sortField 		= $param['sortField'];
		$searchField 		= $param['searchField'];
		$searchText 		= $param['searchText'];
		$searchFromDate		= $param['searchFromDate'];
		$searchToDate 		= $param['searchToDate'];
		$searchDisplay 		= $param['searchDisplay'];

		if($isSession == 0)
		{
			$sortType    			= $this->nsession->get_param('ADMIN_USER','sortType','DESC');
			$sortField   			= $this->nsession->get_param('ADMIN_USER','sortField','date_added');
			$searchField 			= $this->nsession->get_param('ADMIN_USER','searchField','');
			$searchText  			= $this->nsession->get_param('ADMIN_USER','searchText','');
			$searchFromDate 		= $this->nsession->get_param('ADMIN_USER','searchFromDate','');
			$searchToDate  			= $this->nsession->get_param('ADMIN_USER','searchToDate','');
			$searchDisplay  		= $this->nsession->get_param('ADMIN_USER','searchDisplay',10);
		}

		//========= SET SESSION DATA FOR SEARCH / PAGE / SORT Condition etc =====================
		$sessionDataArray = array();
		$sessionDataArray['sortType'] 		= $sortType;
		$sessionDataArray['sortField'] 		= $sortField;
		$sessionDataArray['searchField'] 	= $searchField;
		$sessionDataArray['searchText'] 	= $searchText;
		$sessionDataArray['searchFromDate'] = $searchFromDate;
		$sessionDataArray['searchToDate'] 	= $searchToDate;

		$sessionDataArray['searchDisplay'] 	= $searchDisplay;

		$this->nsession->set_userdata('ADMIN_USER', $sessionDataArray);
		//==============================================================

	    $orderBy = " order by ".$sortField." ".$sortType;
		
		$where = '';

		if($searchField <> "" && $searchText <> "" )
		{
			$where .= " AND ".$searchField. " like '%".$searchText."%' ";
		}

		if($searchFromDate <> "" )
		{
			$where.= " AND date_format(date_added,'%Y-%m-%d') >= '".formate_date($searchFromDate,false,'Y-m-d')."' ";
		}

		if($searchToDate <> "" )
		{
			$where.= " AND date_format(date_added,'%Y-%m-%d') <= '".formate_date($searchToDate,false,'Y-m-d')."' ";
		}

		/*$sql="SELECT COUNT(MM.id) AS TotalrecordCount
			  FROM ".MEMBERMASTER." MM LEFT JOIN ".MEMBERADDRESS." MA ON MM.id=MA.member_id  
		      WHERE 1 $where";*/
		
		$user_session_id = $this->nsession->userdata('user_session_id');
		$sql="SELECT COUNT(id) AS TotalrecordCount FROM ".ADMIN."  WHERE id != '".$user_session_id."' $where";
		$recordSet = $this->db->query($sql);
		
		$config['total_rows'] = 0;
		$config['per_page'] = $searchDisplay;
		if ($recordSet)
		{
         	$row = $recordSet->row();
	   		$config['total_rows'] = $row->TotalrecordCount;
		}
		else
		{
			return false;
		}
		
		if($page > 0 && $page < $config['total_rows'] )
			$start = $page;

		$sql=" SELECT * FROM ".ADMIN." where id != '".$user_session_id."' $where $orderBy  LIMIT ".$start.",".$config['per_page'] ;
			
		$recordSet = $this->db->query($sql);
		
		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
			{
				foreach($row as $key=>$val){
					if(!in_array($key,$isEscapeArr)){
						$recordSet->fields[$key] = outputEscapeString($val);
					}
				}
				$rs[] = $recordSet->fields;			
			}
        }
		else
		{
			return false;
		}
		
		return $rs;
	}
	
    function AddAdminUseredit($id){
	  
	$result = $this->db->query("select * from ".ADMIN." where id = '".$id."'");
	$resultSet = $result->result_array();
	
	if($resultSet){
	    return $resultSet;
	}
    }
    
    function UpdateAdminUser($data,$id){
	
	if($id){
	$sql = "UPDATE ".ADMIN."  SET
				`username`				= '".$data['username']."',
				`password`				= '".$data['password']."',
				`email_address` 		= '".$data['email_address']."',
				`full_name`				= '".$data['full_name']."',
				`status`				= '".$data['status']."',
				`date_added`			= NOW()  
				WHERE id 			= ".$id."";
	
		$result = $this->db->query($sql);
	
	//$this->db->where('id', $id);
	//$this->db->update(ADMIN, $data); 
	
	}
	return true;
    }

    function delete($data){
	
	if($data){
	$this->db->delete(ADMIN,$data); 
	
	}
	return true;
    }
    
    function changeStatus($id, $status='')
	{	
		if($status == ''){
			$sql = "UPDATE ".ADMIN." SET status = if(status = 'Active','Inactive','Active') WHERE id = ".$id."";
		}else{
			 $sql = "UPDATE ".ADMIN." SET status = '".$status."' WHERE id = ".$id.""; 
		}

		$recordSet = $this->db->query($sql);

		if (!$recordSet )
		{
			return false;
		}
    }
	
    function deleteALL($id)
    {
	$sql = "DELETE FROM `".ADMIN."` WHERE `id` = ".$id."";
	$recordSet = $this->db->query($sql);

	if (!$recordSet )
	{
		return false;
	}

	$sql = "DELETE FROM `".ADMIN."` WHERE `id` = ".$id."";
	$recordSet = $this->db->query($sql);
    }
    
    function UpdateUsername_check($str,$do_addedit_id){
	
	$this->db->where('username', $str);
	$this->db->where('id !=', $do_addedit_id); 
	$this->db->from(ADMIN);
	$count = $this->db->count_all_results();
	
	return $count;
	
    }
    
}